"""
告警数据模型
"""
from sqlalchemy import Column, String, Integer, ForeignKey, Text, DateTime, JSON
from sqlalchemy.orm import relationship
from models.base import BaseModel


class Alert(BaseModel):
    """告警模型"""
    __tablename__ = "alerts"
    
    camera_id = Column(Integer, ForeignKey("cameras.id"), nullable=False, comment="摄像头ID")
    ai_result_id = Column(Integer, ForeignKey("ai_results.id"), comment="AI结果ID")
    alert_type = Column(String(50), nullable=False, comment="告警类型")
    severity = Column(String(20), nullable=False, comment="严重程度")
    title = Column(String(200), nullable=False, comment="告警标题")
    description = Column(Text, comment="告警描述")
    status = Column(String(20), default="active", comment="告警状态")
    handled_by = Column(Integer, ForeignKey("users.id"), comment="处理人ID")
    handled_at = Column(DateTime, comment="处理时间")
    extra_data = Column(JSON, comment="告警元数据")
    
    # 关联关系
    camera = relationship("Camera", back_populates="alerts")
    ai_result = relationship("AIResult", back_populates="alerts")
    handler = relationship("User", back_populates="handled_alerts")
    
    def __repr__(self):
        return f"<Alert(id={self.id}, type='{self.alert_type}', severity='{self.severity}')>"